package net.sxnic.item.action;

import javax.persistence.MappedSuperclass;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.util.WebUtils;
import net.sxnic.comm.utils.PropertyUtil;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.action.MdataAction;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.user.User;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.task.TaskManager;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 申报单位或个人 列表
 * 
 * @author yfsun
 * 
 */
@Controller
@RequestMapping(value="/app")
public class ApproveList extends MdataAction {

	private Page datas;
	private String userMoreItem = "true";//用户是否可以创建多个项目:true-可以创建，false-不可以创建
	//用户是否可以创建项目，不在限时内不能
	private String userCreateItem = "false";
	private String userModifyItem = "false";
	private String itemId;

	@Autowired
	private TaskManager taskManager;

	@RequestMapping(value="approve_list.jspa",method={RequestMethod.GET,
			RequestMethod.POST })
	public String execute(ModelMap model) throws Exception {
		//判断是否在申报时限内
		if(checkAppDate())
		{
			userCreateItem = "true";
		}
		//年份处理，若属性表中未定义，则读取当前年
		String currentYear = PropertyUtil.getProperty("item.currentyear");
		if(StringUtils.isBlank(currentYear)) {
			currentYear = ItemConstants.APP_YEAR;
		}
		
		if(StringUtils.isBlank(appYear)){
			appYear = currentYear;			
		}
		
		//操作年，只有操作年=申报书里的年份，才能提交修改，否则只能查看
		request.setAttribute("optYear", this.getJhYear(currentYear));
		
		try {
			catalogAlias = ItemConstants.SYSTEM_CALALOG_ALIAS;
			
			int pageNumber = WebUtils.getPageNumber(request);
			int pageSize = WebUtils.getPageSize(request);

			User user = ucenterService.findUserByUserName(AppContext
					.getUserName(request));
			
			DetachedCriteria dc = DetachedCriteria.forClass(Task.class);
			dc.createAlias("mdata", "mdata");		
			dc.add(Restrictions.eq("assignee", user.getUsername()));
			dc.add(Restrictions.in("status", new String[]{Task.TASK_STATUS_ACTIVE,Task.TASK_STATUS_ENDED}));
			dc.add(Restrictions.eq("lastOne", true));
			
			if(StringUtils.isNotBlank(appYear)){
				dc.add(Restrictions.eq("mdata.appYear", this.getJhYear(appYear)));			
			}
			
			if(StringUtils.isNotBlank(catalogAlias)){
				dc.add(Restrictions.eq("mdata.catalog1", catalogAlias));
			}
			
			if(StringUtils.isNotBlank(catalogCode)){
				dc.add(Restrictions.eq("mdata.catalog2", catalogCode));
			}
			
			datas = taskManager.getPageByDetachedCriteria(pageNumber, pageSize, dc);

			//每个用户可以创建多个项目：1或者n
			String pt = PropertyUtil.getProperty("item.userMoreItem");
			pt = (StringUtils.isBlank(pt))?"":pt;
			//仅当已创建数据且每用户仅可以创建一个项目时，限制创建申报项目
			if(datas!=null && datas.getResults()!=null && datas.getResults().size()>0) {
				if(StringUtils.isBlank(pt) || "1".equals(pt)) {
					userMoreItem = "false";
				}
			}
			//是否允许非当前年创建项目:true-允许，false-不允许
			pt = PropertyUtil.getProperty("item.allowCreateNotCurrYear");
			if(!("false".equalsIgnoreCase(pt) && appYear.equalsIgnoreCase(currentYear))) {
				userMoreItem = "false";
			}
			itemId = mdManager.getCurrentItem(catalogAlias,user.getUsername(), getJhYear(appYear));
			if(StringUtils.isNotBlank(itemId))
			{
				userModifyItem = "true";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		model.addAttribute("datas", datas);
		
		return SUCCESS;
	}

	public Page getDatas() {
		return datas;
	}

	public void setDatas(Page datas) {
		this.datas = datas;
	}

	public String getUserMoreItem() {
		return userMoreItem;
	}

	public void setUserMoreItem(String userMoreItem) {
		this.userMoreItem = userMoreItem;
	}

	public String getUserCreateItem() {
		return userCreateItem;
	}

	public void setUserCreateItem(String userCreateItem) {
		this.userCreateItem = userCreateItem;
	}

	public String getUserModifyItem() {
		return userModifyItem;
	}

	public void setUserModifyItem(String userModifyItem) {
		this.userModifyItem = userModifyItem;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}
}
